package role

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sanrenx.com/xyxf/pkg/e"
)

// 管理模式
func Admin() gin.HandlerFunc {
	return func(c *gin.Context) {
		var code int
		var data interface{}

		code = e.ErrorAuthPermissionToken
		value, exists := c.Get("role")
		if exists && value == "admin" {
			code = e.SUCCESS
		}

		if code != e.SUCCESS {
			c.JSON(http.StatusUnauthorized, gin.H{
				"code": code,
				"msg":  e.GetMsg(code),
				"data": data,
			})

			c.Abort()
			return
		}

		c.Next()
	}
}
